package com.uenpay.im.menu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;

import java.util.ArrayList;

/**
 * @Author: SL
 * @Date: 2016/11/4 16:53
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: 菜单类别
 */
public class MenuAdapter extends BaseAdapter {

    private ArrayList<MenuEntity> arrayList = null;
    private Context mContext;

    public MenuAdapter(Context _Context, ArrayList<MenuEntity> _arr) {
        this.arrayList = _arr;
        mContext = _Context;
    }

    @Override
    public int getCount() {
        return arrayList == null ? 0 : arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList == null ? null : arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MenuViewHodler viewHodler;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_menu_layout, null);
            viewHodler = new MenuViewHodler();
            viewHodler.imageView = (MaskImageView) convertView.findViewById(R.id.menu_iv);
            viewHodler.unread_dynamic_count_tv = (TextView) convertView.findViewById(R.id.unread_dynamic_count_tv);
            viewHodler.name = (TextView) convertView.findViewById(R.id.menu_tv);
            viewHodler.new_dynamic_iv = (ImageView) convertView.findViewById(R.id.new_dynamic_iv);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (MenuViewHodler) convertView.getTag();
        }
        viewHodler.name.setText(arrayList.get(position).name);
        viewHodler.imageView.setImageBitmap(stringtoBitmap(arrayList.get(position).icon_data));
        if (arrayList.get(position).json.indexOf("qyx://app/moments") > 0) {
            viewHodler.unread_dynamic_count_tv.setVisibility(View.VISIBLE);
            setDynamicMsg(viewHodler.imageView, viewHodler.new_dynamic_iv, viewHodler.unread_dynamic_count_tv);
        } else {
            viewHodler.unread_dynamic_count_tv.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    private class MenuViewHodler {
        private TextView name;
        private MaskImageView imageView;
        private TextView unread_dynamic_count_tv;
        private ImageView new_dynamic_iv;
    }

    public Bitmap stringtoBitmap(String string) {
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    /**
     *
     */
    private void setDynamicMsg(MaskImageView new_msg_avatar, ImageView new_dynamic_iv, TextView unread_dynamic_count_tv) {
        String custid = IMApplication.config.getDynamicLastSendCustId();
        int count = IMApplication.config.getDynamicUnreadMsgCount();
        if (!TextUtils.isEmpty(custid)) {
            new_msg_avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(custid,
                    PacketType.PERSONAL), true);
            new_dynamic_iv.setVisibility(View.VISIBLE);
        } else {
            new_msg_avatar.setImageResource(R.drawable.icon_dynamic);
            new_dynamic_iv.setVisibility(View.GONE);
        }

        if (count < 1) {
            unread_dynamic_count_tv.setVisibility(View.INVISIBLE);
        } else {
            if (count <= 99) {
                unread_dynamic_count_tv.setText(count + "");
            } else {
                unread_dynamic_count_tv.setText("99+");
            }
            new_dynamic_iv.setVisibility(View.GONE);
            unread_dynamic_count_tv.setVisibility(View.VISIBLE);
        }

    }
}
